package mbean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import util.FacesUtil;
import vo.OrganizationVO;
import vo.StrategicPerspectiveVO;
import dao.StrategicPerspectiveDAO;
import dao.UsuarioDAO;

public class StrategicPerspectiveMBean extends AbstractMBean{
	
	private List<StrategicPerspectiveVO> listaPerspectiva;
	private String name, description, acronym, nameOrganization;
	private int id, idOrganization, back;
	private List<SelectItem> listOrganization;
	private boolean menuInsertDisabled;
	
	public String search(){
		StrategicPerspectiveDAO dao = new StrategicPerspectiveDAO();
		listaPerspectiva = dao.listPerspectives(this.idOrganization);
		this.menuInsertDisabled = true;
		
		return "strategic-perspective";
	}

	public void setListaPerspectiva(List<StrategicPerspectiveVO> listaPerspectiva) {
		this.listaPerspectiva = listaPerspectiva;
	}

	public List<StrategicPerspectiveVO> getListaPerspectiva() {
		return listaPerspectiva;
	}
	
	public String preparaTelaInsercao(){
		this.name = "";
		this.description = "";
		this.listOrganization = new ArrayList<SelectItem>();
		this.acronym = "";
		this.menuInsertDisabled = false;
		this.id = 0;
		setaTela(true, "strategicPerspective");
		
		if (this.back == 2){
			int idLogin = ((LoginMBean) FacesUtil.getSessionMapValue("login")).getId();
			UsuarioDAO uDAO = new UsuarioDAO();
			Map<String, OrganizationVO> mapa = uDAO.getOrganizations(idLogin);
			
			SelectItem si = new SelectItem();
			si.setLabel("");
			si.setValue("");
			listOrganization.add(si);
			
			for(String key: mapa.keySet()){
				si = new SelectItem();
				si.setLabel(mapa.get(key).getNome());
				si.setValue(mapa.get(key).getId());
				listOrganization.add(si);
			}			
		}
		
		return "insert";
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}
	
	public String save(){
		if (validaTela()){
			StrategicPerspectiveVO spvo = new StrategicPerspectiveVO();
			spvo.setName(this.name);
			spvo.setDescription(this.description);
			
			if (this.id > 0)
				spvo.setId(this.id);
			else
				spvo.setId(-1);
			
			spvo.setAcronym(this.acronym);
			spvo.setIdOrganization(this.idOrganization);			
			
			StrategicPerspectiveDAO dao = new StrategicPerspectiveDAO();
			this.id = dao.saveStrategicPerspective(spvo);
			
			if (this.id == 0){
				FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorInsertStrategicPerspective"));
				return "";
			}
			else if (this.id == -1){
				FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorAcronymExists"));
				return "";
			}
			else{
				this.menuInsertDisabled = true;
				this.idOrganization = 0;
				search();
				return redirecionaBack();
			}
		}
		else{
			return "";
		}
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
	
	private boolean validaTela() {
		boolean retorno = true;
		
		if (this.name.equalsIgnoreCase("")){
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorNameRequired"));
			retorno = false;
		}
		
		if (this.acronym.equalsIgnoreCase("")){
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorAcronymRequired"));
			retorno = false;
		}
		
		return retorno;
	}

	public void setIdOrganization(int idOrganization) {
		this.idOrganization = idOrganization;
	}

	public int getIdOrganization() {
		return idOrganization;
	}

	public void setListOrganization(List<SelectItem> listOrganization) {
		this.listOrganization = listOrganization;
	}

	public List<SelectItem> getListOrganization() {
		return listOrganization;
	}
	
	public boolean getOrganizationSelected(){
		return this.listOrganization.size() == 0;
	}

	public void setAcronym(String acronym) {
		this.acronym = acronym;
	}

	public String getAcronym() {
		return acronym;
	}
	
	public String redirecionaBack(){
		this.menuInsertDisabled = true;
		
		if (this.back == 0){
			return "back";
		}
		else if (this.back == 1){
			return "backIndex";
		}
		else if (this.back == 2){
			return "back";
		}
		else if (this.back == 3){
			if (this.id > 0){
				BSCPerspectiveMBean bscpmb = (BSCPerspectiveMBean) FacesUtil.getSessionMapValue("bscPerspective");			
				bscpmb.usePerspective(this.id);
			}
			setaTela(true, "bscPerspective");
			return "backStrategicPlan";
		}
		else{
			return "back";
		}		
	}

	public void setBack(int back) {
		this.back = back;
	}

	public int getBack() {
		return back;
	}

	public void setNameOrganization(String nameOrganization) {
		this.nameOrganization = nameOrganization;
	}

	public String getNameOrganization() {
		return nameOrganization;
	}

	public void setMenuInsertDisabled(boolean menuInsertDisabled) {
		this.menuInsertDisabled = menuInsertDisabled;
	}

	public boolean getMenuInsertDisabled() {
		return menuInsertDisabled;
	}

	@Override
	public boolean isDirty() {
		return true;
	}
}
